package com.web.base.join;

import com.web.base.user.facade.UserFacade;
import com.web.base.user.message.UserResponse;
import com.web.core.join.DoubleUserJoin;
import com.web.core.join.UserJoin;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户跨库连接信息处理器
 *
 * @author zhengjin
 * @since 1.0 2018年05月15日
 */
public class UserJoinHandler
{
	public UserJoinHandler()
	{
	}

	/**
	 * 跨库连接用户信息
	 *
	 * @param list
	 * @param userFacade
	 */
	public static void userJoin(Collection<? extends UserJoin> list, UserFacade userFacade)
	{
		if (list == null || list.isEmpty() || userFacade == null)
			return;

		Set<Integer> idList = list.stream().map(UserJoin::getUserId).collect(Collectors.toSet());
		Map<Integer, UserResponse> map = userFacade.getList(idList);
		for (UserJoin userJoin : list)
		{
			UserResponse user = map.get(userJoin.getUserId());
			if (user ==  null)
				continue;

			userJoin.setUserMobile(user.getMobile());
			userJoin.setUserName(user.getName());
			userJoin.setUserNickname(user.getNickname());
		}
	}

	/**
	 * 跨库连接用户信息
	 *
	 * @param userJoin
	 * @param userFacade
	 */
	public static void userJoin(UserJoin userJoin, UserFacade userFacade)
	{
		if (userJoin == null || userFacade == null)
			return;

		UserResponse user = userFacade.get(userJoin.getUserId());
		if (user ==  null)
			return;

		userJoin.setUserMobile(user.getMobile());
		userJoin.setUserName(user.getName());
		userJoin.setUserNickname(user.getNickname());
	}

	/**
	 * 跨库连接双份用户信息
	 *
	 * @param list
	 * @param userFacade
	 */
	public static void doubleUserJoin(Collection<? extends DoubleUserJoin> list, UserFacade userFacade)
	{
		if (list == null || list.isEmpty() || userFacade == null)
			return;

		Set<Integer> idList = new HashSet<>();
		for (DoubleUserJoin userJoin : list)
		{
			idList.add(userJoin.getUserId());
			idList.add(userJoin.getOtherUserId());
		}
		Map<Integer, UserResponse> map = userFacade.getList(idList);
		for (DoubleUserJoin userJoin : list)
		{
			UserResponse user = map.get(userJoin.getUserId());
			if (user != null)
			{
				userJoin.setUserMobile(user.getMobile());
				userJoin.setUserName(user.getName());
				userJoin.setUserNickname(user.getNickname());
			}
			user = map.get(userJoin.getOtherUserId());
			if (user != null)
			{
				userJoin.setOtherUserMobile(user.getMobile());
				userJoin.setOtherUserName(user.getName());
				userJoin.setOtherUserNickname(user.getNickname());
			}
		}
	}

	/**
	 * 跨库连接双份用户信息
	 *
	 * @param userJoin
	 * @param userFacade
	 */
	public static void doubleUserJoin(DoubleUserJoin userJoin, UserFacade userFacade)
	{
		if (userJoin == null || userFacade == null)
			return;

		Set<Integer> idList = new HashSet<>();
		idList.add(userJoin.getUserId());
		idList.add(userJoin.getOtherUserId());
		Map<Integer, UserResponse> map = userFacade.getList(idList);
		UserResponse user = map.get(userJoin.getUserId());
		if (user != null)
		{
			userJoin.setUserMobile(user.getMobile());
			userJoin.setUserName(user.getName());
			userJoin.setUserNickname(user.getNickname());
		}
		user = map.get(userJoin.getOtherUserId());
		if (user != null)
		{
			userJoin.setOtherUserMobile(user.getMobile());
			userJoin.setOtherUserName(user.getName());
			userJoin.setOtherUserNickname(user.getNickname());
		}
	}

}
